package com.example.springcloudconfig.web;

import com.example.springcloudconfig.bean.User;
import com.example.springcloudconfig.web.feigin.OrderAction;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
public class IndexAction {

    @RequestMapping("index")
    public String index(HttpServletRequest request) {
        int port = request.getServerPort();
        return "index: "+port;
    }


    @Resource
    RestTemplate restTemplate;



     @HystrixCommand(fallbackMethod="hystrixOrder")
    @RequestMapping("order")
    public String order() {
        return restTemplate.getForObject(
                "http://spring-cloud-order/order",
                String.class);
    }


    public String hystrixOrder(){
        return "降级的order方法";
    }



    @RequestMapping("user")
    public String user() {
        return restTemplate.getForObject(
                "http://spring-cloud-user/user",
                String.class);
    }

    @Resource
    OrderAction orderAction;


    @RequestMapping("orderForFeign")
    public String orderForFeign() {
        return orderAction.order();

    }

    @RequestMapping("sayHello1")
    public String sayHello1(String name) {
        return orderAction.hello(name);
    }
    @RequestMapping("sayHello2")
    public String sayHello2(String name) {
        return orderAction.hello1(name);
    }


    @RequestMapping("add")
    public String add(
            @PathVariable("x") int x,
            @PathVariable("y") int y
    ){
        return  ""+x+"+"+y+"="+orderAction.add(x,y);
    }

    @RequestMapping("showme")
    public User showme(User user ,String newName) {
        return orderAction.showme(user,newName);
    }

}
